package uni.stuttgart.openstreetmap;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Process;

public class GPSThread extends HandlerThread{

	public static final long MIN_DISTANCE = 0;
	public static final float MIN_TIME = 0;
	
	private static TileCalculator tc;
	private static LocationManager locationManager;
	private static LocationListener listener;
	
	private static class GPSLocationListener implements LocationListener{
		@Override
		public void onLocationChanged(Location location) {
			System.out.println("GPS Update lat = "+ location.getLatitude()+ "lon = "+location.getLongitude());
			tc.informAboutGPSChange(location);
		}
		@Override
		public void onProviderDisabled(String provider) {	
		}
		@Override
		public void onProviderEnabled(String provider) {
		}
		@Override
		public void onStatusChanged(String provider, int status,Bundle extras) {
		}
	}
	
	public GPSThread(UserInterface ui, TileCalculator tc) {
		super("GPSThreadThread",Process.THREAD_PRIORITY_BACKGROUND);
		GPSThread.tc = tc;
		this.start();
		
		listener = new GPSLocationListener();
		locationManager = (LocationManager) ui.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener, this.getLooper());
	}

	public void unregisterListeners(){
		locationManager.removeUpdates(listener);
	}
	

}
